package com.ruoyi.information.service.pc.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.information.domain.pc.*;
import com.ruoyi.information.mapper.pc.*;
import com.ruoyi.information.service.pc.ICollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 收藏Service业务层处理
 *
 * @author ruoyi
 * @date 2023-06-13
 */
@Service
public class CollectionServiceImpl implements ICollectionService {
    @Autowired
    private CollectionMapper collectionMapper;

    @Autowired
    private ServiceArticleMapper serviceArticleMapper;

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private CloudClassroomMapper cloudClassroomMapper;

    /**
     * 查询收藏
     *
     * @param id 收藏主键
     * @return 收藏
     */
    @Override
    public Collection selectCollectionById(Long id) {
        return collectionMapper.selectCollectionById(id);
    }

    /**
     * 查询收藏列表
     *
     * @param collection 收藏
     * @return 收藏
     */
    @Override
    public List<Object> selectCollectionList(Collection collection) {
        List<Object> list = new ArrayList<>();
        collection.setUserId(SecurityUtils.getUserId());
        if (collection.getCollectionType() != null) {
            if (collection.getCollectionType() == 2) {
                /*资讯*/
                List<Collection> collections = collectionMapper.selectCollectionList(collection);
                for (Collection c : collections) {
                    Article article = articleMapper.selectArticleById(c.getCollectionVal());
                    if (article != null) {
                        list.add(article);
                    } else {
                        article = new Article();
                        article.setId(c.getId());
                        article.setTitle("已被删除");
                        list.add(article);
                    }
                }
            } else if (collection.getCollectionType() == 3) {
                /*服务资讯*/
                List<Collection> collections = collectionMapper.selectCollectionList(collection);
                for (Collection c : collections) {
                    ServiceArticle serviceArticle = serviceArticleMapper.selectServiceArticleById(c.getCollectionVal());
                    if (serviceArticle != null) {
                        list.add(serviceArticle);
                    } else {
                        serviceArticle = new ServiceArticle();
                        serviceArticle.setId(c.getId());
                        serviceArticle.setTitle("已被删除");
                        list.add(serviceArticle);
                    }
                }
            } else if (collection.getCollectionType() == 4) {
                /*云课堂*/
                List<Collection> collections = collectionMapper.selectCollectionList(collection);
                for (Collection c : collections) {
                    CloudClassroom classroom = cloudClassroomMapper.selectCloudClassroomById(c.getCollectionVal());
                    if (classroom != null) {
                        list.add(classroom);
                    } else {
                        classroom = new CloudClassroom();
                        classroom.setId(c.getId());
                        classroom.setTitle("已被删除");
                        list.add(classroom);
                    }
                }
            } else {
                list.addAll(collectionMapper.selectCollectionList(collection));
            }
        } else {
            list.addAll(collectionMapper.selectCollectionList(collection));
        }
        return list;
    }

    /**
     * 新增收藏
     *
     * @param collection 收藏
     * @return 结果
     */
    @Override
    public int insertCollection(Collection collection) {
        Integer size = collectionMapper.isCollection(collection);
        /*判断是否已收藏*/
        if (size > 0) {
            return 1;
        }
        collection.setCreateTime(DateUtils.getNowDate());
        return collectionMapper.insertCollection(collection);
    }

    /**
     * 修改收藏
     *
     * @param collection 收藏
     * @return 结果
     */
    @Override
    public int updateCollection(Collection collection) {
        Integer size = collectionMapper.isCollection(collection);
        /*判断是否已收藏*/
        if (size > 0) {
            return collectionMapper.cancelCollection(collection);
        }
        return 1;
    }

    /**
     * 批量删除收藏
     *
     * @param ids 需要删除的收藏主键
     * @return 结果
     */
    @Override
    public int deleteCollectionByIds(Long[] ids) {
        return collectionMapper.deleteCollectionByIds(ids);
    }

    /**
     * 删除收藏信息
     *
     * @param id 收藏主键
     * @return 结果
     */
    @Override
    public int deleteCollectionById(Long id) {
        return collectionMapper.deleteCollectionById(id);
    }
}
